//
//  ShoppingCartCell.swift
//  RPay
//
//  Created by Charlie Liao on 2017/7/4.
//  Copyright © 2017年 Charlie. All rights reserved.
//

import UIKit

class ShoppingCartCell: UITableViewCell {
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        selectImgView.isHighlighted = selected
        // Configure the view for the selected state
    }
    
    // MARK: - Event respond
    /// 点击减号
    @IBAction func tapMinusBtn() {
        superview!.cancelKeyBoard()
        var amount = Int(amountTF.text!)
        if amount != nil {
            amount = amount!-1
            minusBtn.isEnabled = amount! > 1
            amountTF.text = String(amount!)
            goods.num = amount!
            totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
            if amountDidChange != nil {
                amountDidChange!(self, goods)
            }
        }
    }
    
    /// 点击加号
    @IBAction func tapPlusBtn() {
        superview!.cancelKeyBoard()
        var amount = Int(amountTF.text!)
        if amount != nil {
            amount = amount!+1
            minusBtn.isEnabled = amount! > 1
            amountTF.text = String(amount!)
            goods.num = amount!
            totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
            if amountDidChange != nil {
                amountDidChange!(self, goods)
            }
        }
    }
    
    @IBAction func amountTFValueChange() {
        let amount = Int(amountTF.text!)
        minusBtn.isEnabled = amount != nil && amount! > 1
    }
    
    @IBAction func amountTFEndEdit() {
        var amount = Int(amountTF.text!)
        if amount == nil || amount! == 0 {
            amountTF.text! = "1"
            amount = 1
        }
        totalPriceLabel.text = String(format: "%.2f", goods.price*Double(amount!))
        
        if amount != goods.num {
            goods.num = amount!
            if amountDidChange != nil {
                amountDidChange!(self, goods)
            }
        }
    }
    
    @IBAction func tapDeleteBtn() {
        if tapDelete != nil {
            tapDelete!(self, goods)
        }
    }
    
    // MARK: - Get Set
    
    var goods: GoodsModel! {
        didSet {
            nameLabel.text = goods.name
            serialNumLabel.text = LocalStr("商品编号:").appending(goods.id)
            unitPriceLabel.text = goods.priceStr
            totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
            amountTF.text = goods.num.description
            minusBtn.isEnabled = goods.num > 1
        }
    }
    
    var tapDelete: ((_ cell: ShoppingCartCell, _ goods: GoodsModel)->Void)?
    var amountDidChange: ((_ cell: ShoppingCartCell, _ goods: GoodsModel)->Void)?
    
    @IBOutlet weak var selectImgView: UIImageView!
    
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var serialNumLabel: UILabel!
    @IBOutlet weak var unitPriceLabel: UILabel!
    @IBOutlet weak var totalPriceLabel: UILabel!
    @IBOutlet weak var deleteBtn: UIButton!
    
    @IBOutlet weak var minusBtn: UIButton!
    @IBOutlet weak var amountTF: UITextField!
    @IBOutlet weak var plusBtn: UIButton!
}
